package com.hyhy.hycloud.uaa.security.listener;

import com.hyhy.hycloud.uaa.security.SecurityUser;
import com.hyhy.hycloud.uaa.service.LoginAttemptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

/**
 * @author Xuegui Yuan
 * @date 2019-02-25
 */
@Component
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Autowired
    LoginAttemptService loginAttemptService;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        SecurityUser securityUser = (SecurityUser) event.getAuthentication().getPrincipal();
        loginAttemptService.loginSuccess(securityUser.getUsername());
    }
}
